package com.zjl.hiring.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zjl.hiring.company.dao.CompanyDao;
import com.zjl.hiring.company.entity.CompanyEntity;

public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private CompanyDao companyDao = new CompanyDao();    
    
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if("login".equals(command)) {
			login(request, response);
		
		} else if ("register".equals(command)) {
			register(request, response);
		} else {
			throw new RuntimeException("请求失败！");
		}
		
	}
	/**
	 * 登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void login(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		
		CompanyEntity company = companyDao.login(code,password);
		if(company == null) {
			response.sendRedirect("view/company/login.jsp?loginError=true");
		} else {
			request.getSession().setAttribute("company",company);
			request.getRequestDispatcher("view/company/main.jsp").forward(request, response);
		}
		
	}
	
	/**
	 * 企业注册
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1.获取参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String linkman = request.getParameter("linkman");
		String address = request.getParameter("address");
		int numbers = Integer.parseInt(request.getParameter("numbers"));
		String slogan = request.getParameter("slogan");
		CompanyEntity company = new CompanyEntity();
		company.setCode(code);
		company.setPassword(password);
		company.setName(name);
		company.setEmail(email);
		company.setPhone(phone);
		company.setLinkman(linkman);
		company.setAddress(address);
		company.setNumbers(numbers);
		company.setSlogan(slogan);
		// 调用业务逻辑
		int row = companyDao.register(company);
		// 转向
		if(row==1) {
			response.sendRedirect("view/company/login.jsp?registerSuccess=true");
		} else {
			response.sendRedirect("view/company/register.jsp?registerError=true");
		}
	}

}
